package controller

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/sessions"

	"im/app"
	"im/requests"
	"im/service"
)

type UsersAccessController struct {
	Session *sessions.Session
	Ctx     iris.Context
	Base
}

// swagger:route POST /users users post-users
// 创建用户
//
// 创建用户
//   Security:
//     app_key:
//   Responses:
//     200: EmptyResponse
func (c *UsersAccessController) Post() {
	var (
		param     = requests.UsersAccessPost{}
		_         = c.Ctx.ReadJSON(&param)
		appsId, _ = c.Ctx.Values().GetUint(app.MIDDLEWARE_APP_ID_KEY)
		token     string
	)
	err := service.Validate.Struct(&param)

	if err != nil {
		c.SendCry(err)
		return
	}

	token, err = service.NewUserService().Create(appsId, param)
	if err != nil {
		c.SendCry(err)
		return
	}
	c.SendSmile(token)
}
